import { purtcPurchaseDetail } from '@/api/url/salesOrderManagement';
import router from '@/router';
import { EFTimeFormatConversion, deepCopy, formatDate, isNullPm } from '@/utils/baseMethods';
import { onMounted, reactive } from 'vue';

export default function importConfig() {
  const base = reactive<any>({
    tabelList: null,
    topTabel: null,
    forTable: null,
  });

  const init = () => {
    getTable();
    base.newData = formatDate(new Date(), 'yyyy/MM/dd');
  };
  /**
   * @exports viewDetails 查看采购单详情
   */
  const getTable = () => {
    const param = router.currentRoute.value.query;
    const steam = {
      TC001: param.type,
      TC002: param.id,
    };
    purtcPurchaseDetail(steam).then((res: any) => {
      if (res.code == 200) {
        base.topTabel = res.data;
        base.bottomTabel = res.data.purtdList;
        base.forTable = [];
        base.forTable = arrayTo2DArray(base.bottomTabel, 3);
        console.log(base.forTable[base.forTable.length - 1].length);
        if (base.forTable[base.forTable.length - 1].length < 2) {
          for (let a = 0; a <= 3 - base.forTable[base.forTable.length - 1].length; a++) {
            base.forTable[base.forTable.length - 1].push({});
          }
        }
        sendWorkOption();
      }
    });
  };
  const arrayTo2DArray = (arr, subArrayLength) => {
    const result: any = [];
    for (let i = 0; i < arr.length; i += subArrayLength) {
      const subArray: any = arr.slice(i, i + subArrayLength);
      result.push(subArray);
    }
    return result;
  };
  /**
   * @exports sendWorkOption 采购单信息嫁接
   */
  const sendWorkOption = () => {
    base.sendWorkTabel = [];
    base.sendWorkTabel = [
      {
        label: '采购单号',
        value: base.topTabel.type + '-' + base.topTabel.id,
      },
      {
        label: '采购人员',
        value: base.topTabel.ProcurementStaffname,
      },
      {
        label: '采购日期',
        value: EFTimeFormatConversion(base.topTabel.purchaseDate),
      },
      {
        label: '供应商编号',
        value: base.topTabel.supplier,
      },
      {
        label: '供应商名称',
        value: base.topTabel.supplierName,
      },
      {
        label: '单据日期',
        value: EFTimeFormatConversion(base.topTabel.documentDate),
      },
      {
        label: '交易币种',
        value: base.topTabel.coinType,
      },
      {
        label: '汇率',
        value: base.topTabel.exchangeRate,
      },
      {
        label: '税额',
        value: base.topTabel.totalTax,
      },
      {
        label: '税前总金额',
        value: base.topTabel.purchaseAmount,
      },
      {
        label: '总金额',
        value: base.topTabel.totalAmount,
      },
      {
        label: '总数量',
        value: base.topTabel.totalNumber,
      },
      {
        label: '审核者',
        value: base.topTabel.userName,
      },
      {
        label: '创建者',
        value: base.topTabel.creator,
      },
      {
        label: '付款条件',
        value: base.topTabel.paymentName,
      },
      {
        label: '备注',
        value: base.topTabel.remark,
      },
    ];
  };

  const timeCange = (param) => {
    return isNullPm(param) ? '' : param.substring(0, 4) + '/' + param.substring(4, 6) + '/' + param.substring(6, 8);
  };

  return {
    base,
    init,
    timeCange,
  };
}
